package com.gps.utilities;

import com.datatypes.GeoPtr;

import android.location.Location;

public class distanceCalculate {

	double distance = 0;
	GeoPtr last;
	Location lastLoc;
	double speed = 0;

	public void getDistance(GeoPtr newPtr) {

		Location loc = new Location("lastLoc");
		loc.setLatitude(newPtr.getLatitude());
		loc.setLongitude(newPtr.getLongitude());
		loc.setAltitude(newPtr.getAltitude());
		loc.setTime(newPtr.getMS());

		double dist = loc.distanceTo(lastLoc);
		distance += dist;
		speed = (dist) / ((loc.getTime() - lastLoc.getTime()) / 1000);
		lastLoc = loc;

	}

	public void getDistance(Location l) {
		double dist = l.distanceTo(lastLoc);
		distance += dist;
		speed = (dist) / ((l.getTime() - lastLoc.getTime()) / 1000);
		lastLoc = l;
	}

	public void setLast(Location l) {
		lastLoc = l;
	}

	public void setLast(GeoPtr gpt) {
		last = gpt;

		lastLoc = new Location("lastLoc");
		lastLoc.setLatitude(gpt.getLatitude());
		lastLoc.setLongitude(gpt.getLongitude());
		lastLoc.setAltitude(gpt.getAltitude());
		lastLoc.setTime(gpt.getMS());
	}

	/** return the calculated speed in km/h **/
	public double getSpeed() {
		if (lastLoc != null)
			return speed * 3.6;
		else
			return 0;
	}

	public CharSequence getDistancStr() {
		return String.format("%.02f", (distance) > 1000 ? (distance) / 1000
				: (distance));
	}

	public double getTotalDistance() {
		return distance;
	}

}
